// pages/unlock_password/unlock_password.js
var config = require('../../config.js')
var CryptoJS = require('./../../util/util.js');

/**
 var CryptoJS = require('./../../util/aes.js')
 var key = CryptoJS.enc.Utf8.parse("xxxxxxx-key");
 var iv = CryptoJS.enc.Utf8.parse("xxxxxxxxx-iv");
 var pwd = CryptoJS.encrypt(this.data.pwdVal, key, iv)
 var original = CryptoJS.encrypt(pwd, key, iv)
 */


Page({

  /**
   * 页面的初始数据
   */
  data: {
      pagerid:1,
      phone: "",
      password:"",
      show_input: false
  },

// wx.reLaunch({
        //   url: '../index/index?id=' + 1,
        // })
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // var jiami = CryptoJS.Encrypt('test')
    // console.log('test AES加密：' + jiami);
    // var jiemi = CryptoJS.Decrypt(jiami);
    // console.log('test AES解密：' + jiemi);

    console.log('onLoad', options)
    console.log('id', options.id)

    var phone = wx.getStorageSync('phone')
    if (((phone != "") && (phone != "")) || (options.id != '')) {
      if(options.id != '')
      {
        phone = options.id
      }
      console.log('phone', phone)
    }
    else
    {
      wx.reLaunch({
        url: '../login/login?id=' + phone,
      })
    }

    this.setData({
      pagerid: options.id,
      phone: phone
    })
  },
  // 冒泡事件
  click_password() {
    this.setData({show_input: true})
  },
  close_show_input() {
    this.setData({ show_input: false });
  },
  close_submit_input (e) {
    console.log('提交事件', e)
    this.setData({
      password: e.detail
    })
  },
  onChange(e){
    // console.log("onChange:",e)
    console.log("onChange:",e.detail)
    this.setData({
      password: e.detail
    })
  },

  onConfirm(e){
    console.log('onConfirm', this.data.password)
    console.log('length', this.data.password.length)
    var _this =  this

    var pwd = CryptoJS.Encrypt(this.data.password)
    if (this.data.password.length == 6) 
    {
         wx.request({
           url: config.url.setUnlockPow,
          method: 'POST',
          data: {
            phone: _this.data.phone,
            password: pwd,
            client: config.client,
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success(res) {
            console.log(res.data)

            if (res.data.code == "200")
            {
              if (_this.data.pagerid == 2) {
                wx.reLaunch({
                  url: '../index/index?id=' + 1,
                })
              }
              else {
                wx.showToast({
                  title: '设置成功',
                  icon: 'success',
                  duration: 2000
                })

                setTimeout(function () {

                  wx.reLaunch({
                    url: '../index/index',
                  })

                }, 200)
              } 
            }
            else
            {
              wx.showToast({
                title: '设置失败',
                icon: 'none',
                duration: 2000
              })
            }
           
          },

        })
    }
    else
    {
      wx.showToast({
        title: '请输入6位数字密码',
        icon: 'none',
        duration: 2000
      })
    }
   

    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {


  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})